<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <span v-if="baTable.form.operate == 'Add' || useAdminInfo().admin_type == 1">
<!--                    <FormItem :label="t('goodsevaluate.user_id')" type="remoteSelect" v-model="baTable.form.items!.user_id" prop="user_id" :input-attr="{ pk: 'xxz_user.id', field: 'nickname', 'remote-url': '/admin/user.User/index' }" :placeholder="t('Please select field', { field: t('goodsevaluate.user_id') })" />-->
                    <FormItem :label="t('goodsevaluate.goods_id')"  type="remoteSelect" v-model="baTable.form.items!.goods_id" prop="goods_id" :input-attr="{ pk: 'xxz_goods.id', field: 'name', 'remote-url': '/admin/Goods/index' }" :placeholder="t('Please select field', { field: t('goodsevaluate.goods_id') })" />
<!--                    <FormItem :label="t('goodsevaluate.order_goods_id')" type="number" prop="order_goods_id" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.order_goods_id" :placeholder="t('Please input field', { field: t('goodsevaluate.order_goods_id') })" />-->
                    <FormItem :label="t('goodsevaluate.nickname')" type="string"  v-model="baTable.form.items!.nickname" prop="nickname" :placeholder="t('Please input field', { field: t('goodsevaluate.nickname') })" />
                    <FormItem :label="t('goodsevaluate.avatar')" type="image"  v-model="baTable.form.items!.avatar" prop="avatar" />


                    <label id="el-id-9093-124" for="el-id-9093-126" class="el-form-item__label" style="width: 160px;margin-bottom: 15px">评分</label>
                    <el-rate v-model="baTable.form.items!.score" allow-half />

                    <FormItem :label="t('goodsevaluate.images')" type="images" v-model="baTable.form.items!.images" prop="images" />
                    <FormItem :label="t('goodsevaluate.content')" type="textarea" v-model="baTable.form.items!.content" prop="content" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('goodsevaluate.content') })" />


                    <FormItem :label="t('goodsevaluate.reply_content')" type="textarea" v-model="baTable.form.items!.reply_content" prop="reply_content" :placeholder="t('Please input field', { field: t('goodsevaluate.reply_content') })" />
                    </span>

                    <span v-if="baTable.form.operate == 'Edit'  && useAdminInfo().admin_type != 1">

                         <el-descriptions title=""  :column="1" style="margin-left: 160px">
<!--                        <el-descriptions-item :label="t('goodsevaluate.goods_id')">{{baTable.form.items!.goods_id}}</el-descriptions-item>-->
                        <el-descriptions-item :label="t('goodsevaluate.nickname')">{{baTable.form.items!.nickname}}</el-descriptions-item>
                        <el-descriptions-item :label="t('goodsevaluate.avatar')">
                            <el-image
                                style="width: 100px; height: 100px"
                                :src="baTable.form.items!.avatar_src"
                                :zoom-rate="1.2"
                                :max-scale="7"
                                :min-scale="0.2"
                                :preview-src-list="[baTable.form.items!.avatar_src]"
                                :initial-index="1"
                                fit="cover"
                            />
                        </el-descriptions-item>
                        <el-descriptions-item :label="t('goodsevaluate.images')">
                            <el-image
                                v-for="(v,i) in baTable.form.items!.images_src"
                                style="width: 100px; height: 100px"
                                :src="v"
                                :zoom-rate="1.2"
                                :max-scale="7"
                                :min-scale="0.2"
                                :preview-src-list="baTable.form.items!.images_src"
                                :initial-index="i"
                                fit="cover"
                            />
                        </el-descriptions-item>
                        <el-descriptions-item :label="t('goodsevaluate.content')">{{baTable.form.items!.content}}</el-descriptions-item>
                        <el-descriptions-item label="评分"><el-rate v-model="baTable.form.items!.score" disabled allow-half /></el-descriptions-item>
                      </el-descriptions>

                    <FormItem :label="t('goodsevaluate.reply_content')" type="textarea" v-model="baTable.form.items!.reply_content" prop="reply_content" :placeholder="t('Please input field', { field: t('goodsevaluate.reply_content') })" />
                    </span>


                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import {useAdminInfo} from "/@/stores/adminInfo";

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    // order_id: [buildValidatorData({ name: 'number', title: t('goodsevaluate.order_id') })],
    // order_goods_id: [buildValidatorData({ name: 'number', title: t('goodsevaluate.order_goods_id') })],
    goods_id: [buildValidatorData({ name: 'required', title: t('goodsevaluate.goods_id') })],
    nickname: [buildValidatorData({ name: 'required', title: t('goodsevaluate.nickname') })],
    // avatar: [buildValidatorData({ name: 'required', title: t('goodsevaluate.avatar') })],
    score: [buildValidatorData({ name: 'required', title: t('goodsevaluate.score') })],
    content: [buildValidatorData({ name: 'required', title: t('goodsevaluate.content') })],
    create_time: [buildValidatorData({ name: 'date', title: t('goodsevaluate.create_time') })],
})
</script>

<style scoped lang="scss"></style>
